package org.jsola.hr.web;

import org.apache.commons.io.FilenameUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.provider.IInsuranceProviderService;
import org.jsola.insurance.entity.QyInsuranceRecordDO;
import org.jsola.insurance.query.QyInsuranceRecordQuery;
import org.jsola.insurance.vo.QyInsuranceRecordVO;
import org.jsola.insurance.vo.RecordListStatisticsVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * @author lyh
 */
@RestController("hrInsuranceController")
@RequestMapping(HrConstants.API_PATH + "/v1/hrInsurance")
public class HrInsuranceController {

    @Autowired
    IInsuranceProviderService insuranceProviderService;

    @PostMapping("/selectPage")
    public Result<Page<QyInsuranceRecordVO>> page(@RequestBody QyInsuranceRecordQuery qyInsuranceRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                insuranceProviderService.selectPage(qyInsuranceRecordQuery, tokenUser)
        );
    }

    @PostMapping("/recordListStatistics")
    public Result<RecordListStatisticsVO> recordListStatistics(@RequestBody QyInsuranceRecordQuery qyInsuranceRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                insuranceProviderService.recordListStatistics(qyInsuranceRecordQuery, tokenUser)
        );
    }

    @PostMapping("/exportList")
    public ResponseEntity<Resource> exportList(@RequestBody QyInsuranceRecordQuery qyInsuranceRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验
        String exportPath = insuranceProviderService.exportRecordList(qyInsuranceRecordQuery, tokenUser);
        // 编码文件名，防止乱码
        String filenames = new String("商保列表导出记录.xlsx".getBytes(StandardCharsets.UTF_8),
                StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(exportPath));
    }

    @GetMapping("/selectEmpInsuranceList/{empId}")
    public Result<List<QyInsuranceRecordVO>> selectEmpInsuranceList(@PathVariable String empId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                insuranceProviderService.selectEmpInsuranceList(empId, tokenUser)
        );
    }
}

